package com.sphd.restaurantsystem.fragments;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.sphd.restaurantsystem.R;
import com.sphd.restaurantsystem.adapters.RechargeAdapter;
import com.sphd.restaurantsystem.base.LazyLoadFragment;
import com.sphd.restaurantsystem.base.RestaurantUrl;
import com.sphd.restaurantsystem.info.VipInfo;
import com.sphd.restaurantsystem.interfaces.RechargeCallBack;
import com.sphd.restaurantsystem.presenter.RechargePresenterImpl;
import com.sphd.restaurantsystem.utils.OnRcvScrollListener;
import com.sphd.restaurantsystem.view.RechargeView;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Hu on 2017/1/19.
 */

public class RechargeFragment extends LazyLoadFragment implements RechargeView {
    @BindView(R.id.my_recy)
    RecyclerView myRecy;
    @BindView(R.id.my_swf)
    SwipeRefreshLayout mySwf;
    int id,pageNo;
    RechargePresenterImpl rechargePresenter;
    int curpageZERO=1;
    int curpageONE=1;
    int curpageTWO=1;

    private Calendar calendar;
    RechargeCallBack rechargeCallBack;
    RechargeAdapter rechargeAdapter;

    boolean hasMore=true;
    @Override
    public int getLayout() {
        return R.layout.swf_and_recy;
    }

    @Override
    public void initViews(View rootView) {
        ButterKnife.bind(this, rootView);
        calendar = Calendar.getInstance();
        rechargePresenter=new RechargePresenterImpl(this);
        initAdapter();
        initEvent();
    }

    private void initEvent() {
        myRecy.setOnScrollListener(new OnRcvScrollListener(){
            @Override
            public void onBottom() {
                super.onBottom();
                // 到底部自动加载
                if (hasMore)
                    addData();
            }
        });


        mySwf.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                if (pageNo==0){
                    curpageZERO=1;
                }else if (pageNo==1){
                    curpageONE=1;
                }else if (pageNo==2) {
                    curpageTWO=1;
                }
                addData();
            }
        });
    }

    private void initAdapter() {
        setRecy(myRecy);
        setRefresh(mySwf);
        rechargeAdapter =new RechargeAdapter(R.layout.recharge_item,new ArrayList<>());
        myRecy.setAdapter(rechargeAdapter);
        rechargeAdapter.openLoadAnimation();
        rechargeAdapter.openLoadAnimation(10);

    }

    @Override
    public void loadData() {
        addData();
    }

    private void addData() {
        rechargePresenter.addData(RestaurantUrl.Httptitle
                + RestaurantUrl.Vip_Charge_List+"cgid="+id+"&curpage="+(pageNo==0?curpageZERO:pageNo==1?curpageONE:curpageTWO)+"&pageno=10"+"&overtime="+getMonthToday(calendar)
        +"&starttime="+(pageNo==0?getMonthToday(calendar):pageNo==1?getMonthFirstDay(calendar):"1991-11-11"));
    }

    public static String getMonthFirstDay(Calendar calendar) {
        return calendar.get(Calendar.YEAR) + "-" +
                ((calendar.get(Calendar.MONTH) + 1) < 10 ? "0" +
                        (calendar.get(Calendar.MONTH) + 1) : (calendar.get(Calendar.MONTH) + 1))
                + "-01";
    }

    public  String getMonthToday(Calendar calendar) {
        return calendar.get(Calendar.YEAR) + "-" +
                ((calendar.get(Calendar.MONTH) + 1) < 10 ? "0" +
                        (calendar.get(Calendar.MONTH) + 1) : (calendar.get(Calendar.MONTH) + 1))
                + "-" + ((calendar.get(Calendar.DAY_OF_MONTH)) < 10 ? "0" +
                calendar.get(Calendar.DAY_OF_MONTH) : calendar.get(Calendar.DAY_OF_MONTH));
    }

    public void setIdType(int id, int pageNo, RechargeCallBack rechargeCallBack) {
        this.id=id;
        this.pageNo=pageNo;

        this.rechargeCallBack=rechargeCallBack;
    }

    @Override
    public void recRes(double daytopup, double alltopup, List<VipInfo> vipInfos) {
        rechargeCallBack.allMoney(alltopup);
        if (pageNo==0){
            if ( curpageZERO==1){
                rechargeAdapter.setNewData(vipInfos);
            }else {
                rechargeAdapter.addData(vipInfos);
            }
            setLoadDataCompleted(true);
        }else if (pageNo==1){
            if ( curpageONE==1){
                rechargeAdapter.setNewData(vipInfos);
            }else {
                rechargeAdapter.addData(vipInfos);
            }
            setLoadDataCompleted(true);
        }else {
            if ( curpageTWO==1){
                rechargeAdapter.setNewData(vipInfos);
            }else {
                rechargeAdapter.addData(vipInfos);
            }
            setLoadDataCompleted(true);
        }
        if (vipInfos.size()!=10){
           hasMore=false;
        }else {
            if (pageNo==0){
                curpageZERO++;
            }else if (pageNo==1){
                curpageONE++;
            }else if (pageNo==2) {
                curpageTWO++;
            }
        }
    }

    @Override
    public void showLoading() {
        showLoadingDialog();
    }

    @Override
    public void dissLoading() {
        dissLoadingDialog();
    }

}
